<?php
//此页面实现了城市分类的选择，店铺的添加

function post_type_store()
{
	$args=array(
			'public'=>true,
			'publicly_queryable'=>true,
			'hierarchical' => false,
			'labels'=>array(
    									'name' => _x('店铺信息', 'post type general name'),
    									'singular_name' => _x('店铺', 'post type singular name'),
    									'add_new' => _x('添加新店铺', '店铺'),
    									'add_new_item' => __('添加新店铺'),
    									'edit_item' => __('编辑店铺'),
    									'new_item' => __('新的店铺'),
    								//	'view_item' => __('预览店铺'),
    									'search_items' => __('搜索店铺'),
    									'not_found' =>  __('您还没有发布店铺'),
    									'not_found_in_trash' => __('回收站中没有店铺信息'), 
    									'parent_item_colon' => ''
  										),
                             'show_ui' => true,
							 'menu_position'=>5,
							 'supports' => array(
												'title',
												'author', 
												//'excerpt',
												//'thumbnail',
												//'comments',
												//'custom-fields',
												//'revisions'
													) ,
							'show_in_nav_menus'	=> true ,
							'taxonomies'		=> array('menutype')	
			);
	register_post_type('store',$args);	
}

add_action('init',post_type_store);
function admin_init(){
	
	add_meta_box("et_post_meta", "店铺详细信息", "et_post_meta", "store", "normal", "high");
}
add_action("admin_init", "admin_init");

function et_post_meta($callback_args) {
	global $post;
	$thumbs = array();
	$custom    = get_post_custom($post->ID);
	$city1     = isset($custom["city1"][0]) ? $custom["city1"][0] : '';
	$city2     = isset($custom["city2"][0]) ? $custom["city2"][0] : '';
	$address   = isset($custom["address"][0]) ? $custom["address"][0] : '';
	$shopname  =isset($custom["shopname"][0]) ? $custom["shopname"][0] : '';
	$ordernum  =isset($custom["ordernum"][0]) ? $custom["ordernum"][0] : '999';
	
	
 ?>
	
<p style="margin-bottom: 22px;">
    	<label for="et_area">所在区域：&nbsp;</label><br/>
        <select name="et_city1" id="et_city1"  onchange="changepro('et_city2','et_city1');">
        <?php if(empty($city1)){echo "   <option value='' selected>省/直辖市</option>";}else{echo "<option value='$city1'>$city1</option>";}?>		  <option value='北京市'>北京市</option>
              <option value='天津市'>天津市</option>
              <option value='河北省'>河北省</option>
              <option value='山西省'>山西省</option>
              <option value='内蒙古区'>内蒙古区</option>
              <option value='辽宁省'>辽宁省</option>
              <option value='吉林省'>吉林省</option>
              <option value='黑龙江省'>黑龙江省</option>
              <option value='上海市'>上海市</option>
              <option value='江苏省'>江苏省</option>
              <option value='浙江省'>浙江省</option>
              <option value='安徽省'>安徽省</option>
              <option value='福建省'>福建省</option>
              <option value='江西省'>江西省</option>
              <option value='山东省'>山东省</option>
              <option value='河南省'>河南省</option>
              <option value='湖北省'>湖北省</option>
              <option value='湖南省'>湖南省</option>
              <option value='广东省'>广东省</option>
              <option value='广西区'>广西区</option>
              <option value='海南省'>海南省</option>
              <option value='重庆市'>重庆市</option>
              <option value='四川省'>四川省</option>
              <option value='贵州省'>贵州省</option>
              <option value='云南省'>云南省</option>
              <option value='西藏区'>西藏区</option>
              <option value='陕西省'>陕西省</option>
              <option value='甘肃省'>甘肃省</option>
              <option value='青海省'>青海省</option>
              <option value='宁夏区'>宁夏区</option>
              <option value='新疆区'>新疆区</option>
              <option value='台湾省'>台湾省</option>
              <option value='香港特区'>香港特区</option>
              <option value='澳门特区'>澳门特区</option>
          </select>
		  <select name="et_city2" id="et_city2" >
            <?php if(empty($city2)){echo "   <option value='' selected>请选择</option>";}else{echo "<option value='$city2'>$city2</option>";}?>
          </select>
			<br/>
           <small>(请选择商铺所在的区域)</small>
    </p>

	<p style="margin-bottom: 22px;">
		<label for="et_address">店铺地址: </label><br/>
		<input id="et_address" name="et_address"  style="width: 90%" value="<?php echo $address;?>"/><br/>
		<small>(店铺的地址)</small>
	</p>
    
    <p style="margin-bottom: 22px;">
		<label for="et_shopname">店铺名称: </label><br/>
		<input id="et_shopname" name="et_shopname"  style="width: 90%" value="<?php echo $shopname; ?>" /><br/>
		<small>(店铺的名称)</small>
	</p>
    
     <p style="margin-bottom: 22px;">
		<label for="et_ordernum">店铺排序: </label><br/>
		<input id="et_ordernum" name="et_ordernum" style="width:5%" value="<?php echo $ordernum; ?>" onKeyUp="value=value.replace(/[^0-9]/g,'')" /><br/>
		<small>(店铺排序，数字越小，越排前)</small>
	</p>

	<?php
}




function save_details($post_id){
	global $post;
	
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
		return $post_id;
		
	if (isset($_POST["et_city1"]) && $_POST["et_city1"] <> '') update_post_meta($post->ID, "city1", $_POST["et_city1"]);
	if (isset($_POST["et_city2"]) && $_POST["et_city2"] <> '') update_post_meta($post->ID, "city2", $_POST["et_city2"]);
	if (isset($_POST["et_city3"]) && $_POST["et_city3"] <> '') update_post_meta($post->ID, "city3", $_POST["et_city3"]);
	
	
	if (isset($_POST["et_address"]) && $_POST["et_address"] <> '') update_post_meta($post->ID, "address", $_POST["et_address"]);
	if (isset($_POST["et_shopname"]) && $_POST["et_shopname"] <> '') update_post_meta($post->ID, "shopname", $_POST["et_shopname"]);
	if (isset($_POST["et_ordernum"]) && $_POST["et_ordernum"] <> '') update_post_meta($post->ID, "ordernum", $_POST["et_ordernum"]);
	
}
add_action('save_post', 'save_details');

function estore_upload_scripts() {
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_register_script('my-upload', get_bloginfo('template_directory').'/js/custom_uploader.js', array('jquery','media-upload','thickbox'));
	wp_register_script('my_area',get_bloginfo('template_directory').'/js/selectarea.js');
	wp_register_script('rw_post',get_bloginfo('template_directory').'/js/rw_post.js');
	wp_enqueue_script('my-upload');
	wp_enqueue_script('my_area');
	wp_enqueue_script('rw_post');
}
function estore_upload_styles() {
	wp_enqueue_style('thickbox');
}
add_action('admin_print_scripts', 'estore_upload_scripts');
add_action('admin_print_styles', 'estore_upload_styles');
?>
